'use strict'
var info = require('./info');
 info();

var table = require('./table')
var Crawler = require("crawler");
for (let index = 1; index <= 9; index++) {
    var URL = 'https://search.51job.com/list/170000,000000,0000,00,9,99,Javascript,2,' + index + '.html?lang=c&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&ord_field=0&dibiaoid=0&line=&welfare='
    var c = new Crawler({
        maxConnections: 1,
        // This will be called for each crawled page
        callback: function (error, res, done) {
            if (error) {
                console.log(error);
            } else {
                var $ = res.$;

                var list = $('#resultList.dw_table .el').toArray();

                list.forEach(element => {
                    var txt1 = $(element).find('p.t1 span').text().trim().replace(/[ \n\r]/g, "");
                    var txt2 = $(element).find('span.t2 a ').attr('title');
                    var txt3 = $(element).find('span.t3').text().trim();
                    var txt4 = $(element).find('span.t4').text().trim();
                    var txt5 = $(element).find('span.t5').text().trim();
               
                    // console.log(txt1);
                    // console.log(txt2);
                    // console.log(txt3);
                    // console.log(txt4);
                    // console.log(txt5);
                 table.create({ Job_title: txt1, Company_name: txt2, Working_place: txt3, money: txt4, Release_time: txt5 })
                });
                // $ is Cheerio by default
                //a lean implementation of core jQuery designed specifically for the server

            }
            done();
        }
    });
    c.queue(URL);

}